package com.yunkaweilai.android.activity.operation.refund;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.MainActivity;
import com.yunkaweilai.android.activity.web.WebActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.Web;
import com.yunkaweilai.android.model.operation.refund.RefundSelectorModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.InputMethodUtils;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.shop.ShopCodeSingleDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 退款
 * Created by Administrator on 2017/8/8.
 */

public class RefundOrderActivity extends BaseActivity {

    @BindView(R.id.id_edt_search)
    EditText idEdtSearch;
    @BindView(R.id.id_img_clean)
    ImageView idImgClean;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    @BindView(R.id.id_tv_time)
    TextView idTvTime;
    @BindView(R.id.id_tv_order_no)
    TextView idTvOrderNo;
    @BindView(R.id.id_tv_price)
    TextView idTvPrice;
    @BindView(R.id.id_llayout_all)
    LinearLayout idLlayoutAll;
    @BindView(R.id.id_llayout_item)
    LinearLayout idLlayoutItem;
    private Gson gson = new Gson();
    private RefundSelectorModel refundSelectorModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_order);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("退款操作").setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        InputMethodUtils.closeSoftKeyboard(mAc);
                        intent2Activity(MainActivity.class);
                        finish();
                    }
                });
        initClick();
        Utils.autoShowKeyboard(idEdtSearch);
    }

    private void initClick() {
        idEdtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null && idEdtSearch.length() > 0) {
                    //点击搜索要做的操作
                    initHttp();
                    InputMethodUtils.closeSoftKeyboard(mAc);
                }
                return false;
            }
        });

        idEdtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s == null || s.length() == 0) {
                    idImgClean.setVisibility(View.GONE);
                    return;
                } else {
                    idImgClean.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        idImgClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idImgClean.setVisibility(View.GONE);
                idEdtSearch.setText("");
            }
        });
    }


    private void initHttp(){
        HttpRequest.postUrl(Api.ORDER_SEARCH_INFO)
                .addParams("keyword",idEdtSearch.getText().toString())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc,json);
                        if (checkCode){
                            refundSelectorModel = gson.fromJson(json, RefundSelectorModel.class);
                            WebActivity.actionStart(mAc, Web.ORDER_REFUND + "?tid="+refundSelectorModel.getData().getOrderInfo().getTid() +
                                    "&token=" + BaseApplication.getToken());
//                            initView();
                        }else{
                            ShopCodeSingleDialog dialog = new ShopCodeSingleDialog(mAc,"没有查到订单,请核对！");
                            dialog.show();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }

//    private void initView() {
//        final RefundSelectorModel.DataBean.OrderInfoBean orderInfo = refundSelectorModel.getData().getOrderInfo();
//        final String orderType = refundSelectorModel.getData().getOrderType();
//        if ("ConsumeOrder".equals(orderType)){
//            idTvOrderNo.setText(orderInfo.getStrconsume_type() + "：" + orderInfo.getTid());
//        }else if ("RechargeOrder".equals(orderType)){
//            idTvOrderNo.setText(orderInfo.getStrrecharge_type() + "：" + orderInfo.getTid());
//        }
//
//        if (StringUtils.isEmpty(orderInfo.getMember_name())){
//            idTvName.setText("散客");
//        }else{
//            idTvName.setText(orderInfo.getMember_name()+" (" + orderInfo.getLevel_name() + ")");
//        }
//        idTvTime.setText(orderInfo.getCreate_time()+"");
//        idTvPrice.setText(orderInfo.getS_payment()+"");
//
//        idLlayoutItem.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if ("ConsumeOrder".equals(orderType)) {
//                    WebActivity.actionStart(mAc, Web.ORDER_REFUND + "?tid="+orderInfo.getTid() +
//                            "&token=" + BaseApplication.getToken());
//                }else if ("RechargeOrder".equals(orderType)){
//                    WebActivity.actionStart(mAc,Web.ORDER_REFUND + "?tid="+orderInfo.getTid()
//                            + "&token=" + BaseApplication.getToken());
//                }
//            }
//        });
//    }

}
